/**
 * This class is responsible for setting the input data graph
 */
package org.wmine.visualization;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.wmine.data.Tuple;
import org.wmine.visualization.data.AbstractChartDataProvider;
import org.wmine.visualization.data.KMeansData;
import org.wmine.visualization.data.KMeansDataProvider;
import org.wmine.visualization.data.KMeansData.TupleData;
import org.xml.sax.SAXException;

/**
 * @author Rajeev Kumar Thakur
 *
 */
public class DrawKmeansClusteringGraph implements IDrawMiningAlgorithmOutput {

	/* (non-Javadoc)
	 * @see org.wmine.visualization.IDrawMiningAlgorithmOutput#drawGraph()
	 */
	private String outputFile;
	private KMeansData kMeansData;
	private AbstractChartDataProvider chartData;
	private ArrayList<TupleData> clusterList;
	
	public DrawKmeansClusteringGraph(String outputFile) throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {
			this.outputFile = outputFile;
			chartData = new KMeansDataProvider(outputFile);
			kMeansData = (KMeansData)chartData.getChartData();
			
	}

	public SetChartPanel drawGraph() throws ParserConfigurationException,SAXException, IOException {
		// TODO Auto-generated method stub
		JFreeChart chart;
		 chart = setupChartProperties();
		 SetChartPanel setChart = new SetChartPanel(chart);
		 return setChart;
	}
	protected JFreeChart setupChartProperties() {
		JFreeChart chart;
		clusterList = new ArrayList<TupleData>();
		clusterList = kMeansData.getClusterList();
		
		XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
		
		ArrayList<XYSeries> xySeriesList = new ArrayList<XYSeries>();
		for (int i = 0; i < kMeansData.getNoOfClusters(); i++) {
			XYSeries series = new XYSeries("Cluster "+i);
			addSeriesData(series,i);
			xySeriesList.add(series);
		}		
		
		for(int i = 0;i < xySeriesList.size();i++) {
			xySeriesCollection.addSeries(xySeriesList.get(i));
		}
		XYDataset dataset = xySeriesCollection;
		chart = ChartFactory.createXYLineChart(
				"KMeans Clustering Algorithm",      // chart title
				 kMeansData.getAttribute1(),   // x axis label
				 kMeansData.getAttribute2(),  // y axis label
				dataset,                  // data
				PlotOrientation.VERTICAL,
				true,                     // include legend
				true,                     // tooltips
				false                     // urls
		);
		
		chart.setBackgroundPaint(Color.white);
		final XYPlot plot = chart.getXYPlot();
		plot.setBackgroundPaint(Color.white);
		plot.setDomainGridlinePaint(Color.lightGray);
		plot.setRangeGridlinePaint(Color.lightGray);

 		final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		renderer.setSeriesLinesVisible(0, true);
		renderer.setSeriesShapesVisible(1, false);
		plot.setRenderer(renderer);
		
		final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		return chart;		
	}

	private void addSeriesData(XYSeries series,int tupelIndex) {
		TupleData tempTuple = kMeansData.getTupleDataInstance(); 
			tempTuple = clusterList.get(tupelIndex);
		for (int j = 0; j < tempTuple.getXData().size(); j++) {
			series.add(tempTuple.getXData().get(j), tempTuple.getYData().get(j));
		}	
	}
}
